import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from "vue-router";
import store from "../store";
// 导入loading
import { ElLoading } from "element-plus";
import "element-plus/theme-chalk/el-loading.css";
// 导入loading

// 导入路由
import { baserouter } from "./baserouter";
// 导入路由

const routes = [...baserouter,];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

const IsLoad = [];
let loading;

router.beforeEach((to, from, next) => {
  if (IsLoad.indexOf(to.name) == -1) {
    IsLoad.push(to.name);
    // showloading 切换页面显示loading
    loading = ElLoading.service({
      lock: true,
      fullscreen: true,
      text: "Loading",
      background: "rgba(0, 0, 0, 0.7)",
    });
  }
  const token = store.state.token;
  if (token && to.path == "/login") {
    return next("/workbench");
  }
  next();
});

router.afterEach((to, from) => {
  // loaded 关闭loading
  loading.close();
});

export default router;
